#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.common.bean.vo;

import ${package}.common.bean.po.Group;
import ${package}.common.bean.po.Role;
import ${package}.common.bean.po.User;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.List;
import java.util.Optional;

/**
 * Description: User for OAuth sso
 *
 * @author wenxiaopeng
 * @date 2020/04/13
 *
 * <pre>
 *              ${copyright}
 *      Copyright (c) 2019. All Rights Reserved.
 * </pre>
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ApiUser implements Serializable {

    private static final long serialVersionUID = 1L;

    private static final String ROLE_MANAGER = "0001";
    /**
     * 主键
     */
    @ApiModelProperty("主键")
    private Long id;
    /**
     *用户昵称
     */
    @ApiModelProperty("用户昵称")
    private String username;
    /**
     *用户姓名
     */
    @ApiModelProperty("用户姓名")
    private String nickname;
    /**
     *电子邮箱
     */
    @ApiModelProperty("电子邮箱")
    private String email;
    /**
     *职位
     */
    @ApiModelProperty("职位")
    private String job;
    /**
     *用户头像
     */
    @ApiModelProperty("用户头像")
    private String icon;
    /**
     *账户状态：0：禁用，1：启用，2：邮箱未验证，3：手机号未验证
     */
    @ApiModelProperty("账户状态：0：禁用，1：启用，2：邮箱未验证，3：手机号未验证")
    private Integer status;
    /**
     *用户全称
     */
    @ApiModelProperty("用户全称")
    private String fullName;
    /**
     *关联组织表
     */
    @ApiModelProperty("关联组织表")
    private List<Group> groupArrayList;


    @ApiModelProperty("是否管理员")
    private boolean manager = false;

    @ApiModelProperty("用户详情")
    private User details;

    public static ApiUser fromUser(User user){
        ApiUser apiUser = new ApiUser();
        if(null!=user) {
            user.setPassword(null);
            BeanUtils.copyProperties(user, apiUser);
            apiUser.setDetails(user);
            List<Role> roles = user.getRoleArrayList();
            if(CollectionUtils.isNotEmpty(roles)) {
                Optional<Role> any = roles.stream().filter(role -> ROLE_MANAGER.equals(role.getCode())).findAny();
                apiUser.setManager(any.isPresent());
            }
        }
        return apiUser;
    }
}
